package gui.polish;

import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.SystemColor;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JTextField;

import utilities.SendMailSSL;
import utilities.Services;
import clients.RPCClient;
import net.miginfocom.swing.MigLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;

import entities.ConnectParameters;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.Font;

import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;

import java.awt.Color;
/**
 * 
 * @author adam
 *This panel offers the user at the polish phase the option of locating an orderID and it returns an answer where it's located recording to the orderID status at that point.
 */
public class OrderIDSearchPanel extends JPanel{
	
	private JTextField jtf_orderID;
	private JTextField jtf_statusID;
	private OrderIDSearchPanel myOrderIDSerchPanel;
	private PolishFrame myFrame;
	private PolishPanel myPolishPanel;
	private RPCClient orderIDSerachPanelClient;
	private SendMailSSL sendMail;
	private StartNewPolishSerachFrame mySearchFrame;
	private JTextField textField;
	private JTextField textField_1;
	
	/**
	 * Constructor
	 * @param a-myFrame
	 * @param b-PolishPanel
	 * @wbp.parser.constructor
	 */
	public OrderIDSearchPanel(PolishFrame a ,PolishPanel b) {
		setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "\u05D7\u05D9\u05E4\u05D5\u05E9 \u05E4\u05E7''\u05E2", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(new Color(173, 216, 230));
		this.myFrame=a;
		this.myPolishPanel=b;
		initializePanel();

	}
	
	public OrderIDSearchPanel(StartNewPolishSerachFrame mySearchFrame) {
		
		this.mySearchFrame=mySearchFrame;
		initializePanel();
		
	}
/**
 *This method Initializes the panel with the relevant fields 	
 */
	private void initializePanel() {
			
			myOrderIDSerchPanel = this;
			setLayout(new MigLayout("", "[grow][][][100px:n][][grow]", "[grow][][][][][][][][][][][grow]"));
			
			JButton searchButton = new JButton("חפש");
			searchButton.setFont(new Font("Tahoma", Font.PLAIN, 13));
			searchButton.setIcon(new ImageIcon(OrderIDSearchPanel.class.getResource("/graphics/search24.png")));
			searchButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e)
				{
					if (jtf_orderID.getText().isEmpty())
					{
						JOptionPane.showMessageDialog(myFrame,
							    "יש להקיש מספר פק''ע.","שגיאה",JOptionPane.ERROR_MESSAGE);
					}
					else
					{
						search();
					}
					
				}
			});
			add(searchButton, "cell 1 3,alignx center");
			
			jtf_orderID = new JTextField();
			jtf_orderID.addFocusListener(new FocusAdapter() {
				@Override
				public void focusGained(FocusEvent arg0) {
					jtf_statusID.setText("");
				}
				@Override
				public void focusLost(FocusEvent e) {
					Boolean answer = Services.isNumeric(jtf_orderID.getText().toString());
					if(answer == false)
					{
						JOptionPane.showMessageDialog(myFrame, "הזנת ערך שגוי");
						jtf_orderID.setText("");
					}
				}
			});
			
			add(jtf_orderID, "cell 3 3,growx");
			jtf_orderID.setColumns(10);
			
			JLabel label = new JLabel("מספר פק\"ע:");
			label.setFont(new Font("Tahoma", Font.PLAIN, 13));
			add(label, "cell 4 3");
			
			JLabel label_1 = new JLabel("תוצאות ");
			label_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
			add(label_1, "cell 4 5");
			
			JSeparator separator = new JSeparator();
			add(separator, "cell 0 6 5 1,grow");
			
			JButton btnNewButton = new JButton("דווח על פק\"ע חסרה");
			btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 13));
			btnNewButton.setIcon(new ImageIcon(OrderIDSearchPanel.class.getResource("/graphics/notFoundOrder24.png")));
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0)
				{
					if(jtf_orderID.getText().toString().equals(""))
					{
						JOptionPane.showMessageDialog(myPolishPanel,"לא ניתן לדווח ללא בחירה פקע");
					}
					else
					{
						JOptionPane.showMessageDialog(myPolishPanel,
							    "הודעה תשלח לסמנ''כל התפעול..","אישור",JOptionPane.INFORMATION_MESSAGE);
						Thread sendEmail = new Thread()
						{
							
								public void run()
								{
					            	ConnectParameters getmail = new ConnectParameters();
									getmail.readEmail();
									sendMail = new SendMailSSL();
									String strTitle ;
									String strBody ;
									strTitle = "Order report is missing\n";
									strBody = "This is an automated report from the Polish department, order number " +jtf_orderID.getText().toString()+ " has been reported as missing." ;
									sendMail.SendEmail(getmail.getEmail(),strBody,strTitle);

									
								}
						};
				          
						sendEmail.start();
				            
					}
							
						
						
									
				}
			});
			add(btnNewButton, "cell 1 7");
			
			jtf_statusID = new JTextField();
			add(jtf_statusID, "cell 3 7,growx");
			jtf_statusID.setColumns(10);
			
			JLabel label_2 = new JLabel("סטטוס:");
			label_2.setFont(new Font("Tahoma", Font.PLAIN, 13));
			add(label_2, "cell 4 7");
			
			JButton btnNewButton_1 = new JButton("חזרה");
			btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
			btnNewButton_1.setIcon(new ImageIcon(OrderIDSearchPanel.class.getResource("/graphics/back.png")));
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try
					{
						myFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
						myFrame.setContentPane(myPolishPanel);
					}
					catch(Exception e1)
					{
						mySearchFrame.dispose();
					}
				}
			});
			add(btnNewButton_1, "cell 1 10,alignx center");
	}
/**
 * This method searches the DB for the right otrderID and it's place .
 * 
 */
	private int search()
	{
		ArrayList<Object> message = new ArrayList<>();
		ArrayList<Object> respond = new ArrayList<>();
		message.add("searchOrderID");
		message.add(jtf_orderID.getText().toString());
		try{
			orderIDSerachPanelClient = new RPCClient();
			respond = orderIDSerachPanelClient.call(message);
		}
		catch(Exception e)
		{
			//e.printStackTrace();
		}
		if(respond.get(1).equals("valid"))
		{
			String str = translate(respond.get(2).toString());
			jtf_statusID.setText(str);
		}
		else
		{
			jtf_statusID.setText("הפקע לא נמצאה");			
		}
		return 0 ;
	}
	private String translate(String a)
	{
		
		String str = a;
		switch (str)
		{
			case "0" :
			return "test";
			case "1" :
				return "פקע חדשה ממתינה לשלב החריטה";
			case "2" :
				return "נחרטה ממתינה להפרדה";
			case "3" :
				return "הופרדה ממתינה לפוליש";
			case "31" :
				return "בשלב הפוליש";
			case "4" :
				return "סיימה פילוש , ממתינה לשלב הביקורת";
			case "5" :
				return "הוחזרה לפוליש";
			case "6" :
				return "נבדקה , ממתינה לאריזה";
			case "7" :
				return "הוחזרה לבדיקה";
			case "81" :
				return "במהלך אריזה";
			case "99" :
				return "סיימה אריזה";
		}
		return "not found";		
	}
	
	@Override
	public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Color color1 = getBackground();
    Color color2 = color1.darker();
    int w = getWidth();
    int h = getHeight(); 
    GradientPaint gp = new GradientPaint(
            0, 0, color1,
            0, h, color2);

    g2d.setPaint(gp);
    g2d.fillRect(0, 0, w, h);
    }
}
